package com.photoeditor.demo.ui.widget.image.edit;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

import com.photoeditor.R;
import com.photoeditor.demo.ui.activity.edit.EditorImageActivity;
import com.photoeditor.demo.ui.widget.image.eraser.ImageEraserView;
import com.photoeditor.demo.ui.widget.image.hair.CustomSizeSeekBar;
import com.photoeditor.demo.ui.widget.image.hair.OnSeekBarChangeListener;


public class EraserBarView extends LinearLayout implements IDealInit, StickerSettingTopView.StickerTopTabListener, ImageEraserView.EraserListener, OnSeekBarChangeListener {

    private ImageEraserView mEraserView;

    private StickerSettingTopView mSettingTopView;

    private EditorImageActivity mActivity;

    private CustomTabButton mEraseButton;
    private CustomTabButton mRepairButton;
    private CustomTabButton mExchangeButton;

    private View mSeekBarLayoutErase;
    private CustomSizeSeekBar mSizeSeekBarErase;
    private View mSeekBarLayoutRepair;
    private CustomSizeSeekBar mSizeSeekBarRepair;

    private Animation mAnimationIn;
    private Animation mAnimationOut;

    private int mEraseProgress;
    private int mRepairProgress;
    private boolean mEraseSeekBarShow;
    private boolean mRepairSeekBarShow;

    private static final int DEFAULT_PROGRESS = 30;

    public EraserBarView(Context context) {
        super(context);
    }

    public EraserBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EraserBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    @Override
    public void init() {
        initViews();
    }

    /**
     * 初始化
     */
    private void initViews() {
        hookupGraffitoButtons();
        initView(getResources().getColor(R.color.image_edit_sencond_text_color),
                getResources().getColor(R.color.accent_color));
        mEraseProgress = DEFAULT_PROGRESS;
        mRepairProgress = DEFAULT_PROGRESS;
        mEraseSeekBarShow = true;
        mRepairSeekBarShow = false;
        checkEraseButton();
    }

    private OnClickListener mClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.btn_eraser_erase) {
                checkEraseButton();

                showSeekBarWithAnim(mSeekBarLayoutRepair, false, false);
                showSeekBarWithAnim(mSeekBarLayoutErase, !mEraseSeekBarShow, !(mRepairSeekBarShow && !mEraseSeekBarShow));
                mEraseSeekBarShow = !mEraseSeekBarShow;
                mRepairSeekBarShow = false;

                mEraserView.setPaintStyle(ImageEraserView.PAINT_STYLE_ERASE);
            } else if (id == R.id.btn_eraser_repair) {
                checkRepairButton();

                mSeekBarLayoutErase.setVisibility(GONE);

                showSeekBarWithAnim(mSeekBarLayoutErase, false, false);
                showSeekBarWithAnim(mSeekBarLayoutRepair, !mRepairSeekBarShow, !(mEraseSeekBarShow && !mRepairSeekBarShow));
                mRepairSeekBarShow = !mRepairSeekBarShow;
                mEraseSeekBarShow = false;

                mEraserView.setPaintStyle(ImageEraserView.PAINT_STYLE_REVERSE);
            } else if (id == R.id.btn_eraser_exchange) {
                mEraserView.exchange();
            }

        }
    };

    private void showSeekBarWithAnim(final View view, boolean show, boolean anim) {
        if (show && view.getVisibility() != VISIBLE) {
            view.setVisibility(VISIBLE);
            if (anim) {
                view.startAnimation(getAnimationIn());
            }
        } else if (!show && view.getVisibility() == VISIBLE) {
            if (anim) {
                Animation animationOut = getAnimationOut();
                animationOut.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        view.setVisibility(GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                view.startAnimation(animationOut);
            } else {
                view.setVisibility(GONE);
            }
        }
    }

    private void hookupGraffitoButtons() {

        mEraseButton = (CustomTabButton) findViewById(R.id.btn_eraser_erase);
        mEraseButton.setOnClickListener(mClickListener);

        mRepairButton = (CustomTabButton) findViewById(R.id.btn_eraser_repair);
        mRepairButton.setOnClickListener(mClickListener);

        mExchangeButton = (CustomTabButton) findViewById(R.id.btn_eraser_exchange);
        mExchangeButton.setOnClickListener(mClickListener);

        mSeekBarLayoutErase = findViewById(R.id.ll_size_seek_erase);
        mSizeSeekBarErase = findViewById(R.id.size_seekbar_erase);
        mSizeSeekBarErase.setOnSeekBarChangeListener(this);
        mSizeSeekBarErase.setMax(100);
        mSizeSeekBarErase.setProgress(DEFAULT_PROGRESS);

        mSeekBarLayoutRepair = findViewById(R.id.ll_size_seek_repair);
        mSizeSeekBarRepair = findViewById(R.id.size_seekbar_repair);
        mSizeSeekBarRepair.setOnSeekBarChangeListener(this);
        mSizeSeekBarRepair.setMax(100);
        mSizeSeekBarRepair.setProgress(DEFAULT_PROGRESS);
    }

    private void checkEraseButton() {
        mEraseButton.setChecked(true);
        mRepairButton.setChecked(false);
        mSizeSeekBarErase.setProgress(mEraseProgress);
    }

    private void checkRepairButton() {
        mEraseButton.setChecked(false);
        mRepairButton.setChecked(true);
        mSizeSeekBarRepair.setProgress(mRepairProgress);
    }

    private Animation getAnimationIn() {
        if (mAnimationIn == null) {
            mAnimationIn = AnimationUtils.loadAnimation(getContext(), R.anim.bottom_in);
        } else {
            mAnimationIn.reset();
        }
        return mAnimationIn;
    }

    private Animation getAnimationOut() {
        if (mAnimationOut == null) {
            mAnimationOut = AnimationUtils.loadAnimation(getContext(), R.anim.bottom_out);
        } else {
            mAnimationOut.reset();
        }
        return mAnimationOut;
    }

    public void initView(int primaryColor, int emphasisColor) {

        mEraseButton.setThemeImageRes(R.drawable.ic_tool_eraser,
                R.drawable.ic_tool_eraser_light);
        mEraseButton.setTextColor(primaryColor, emphasisColor);
        mRepairButton.setThemeImageRes(R.drawable.ic_tool_eraser_reset,
                R.drawable.ic_tool_eraser_reset_light);
        mRepairButton.setTextColor(primaryColor, emphasisColor);

        mExchangeButton.setThemeImageRes(R.drawable.ic_tool_eraser_change, View.NO_ID);
        mExchangeButton.setTextColor(primaryColor, primaryColor);

    }

    public void setEraserView(ImageEraserView eraserView) {
        mEraserView = eraserView;
        mEraserView.setEraserListener(this);
    }

    public void setSettingTopView(StickerSettingTopView settingTopView) {
        mSettingTopView = settingTopView;
        mSettingTopView.setStickerTopTabListener(this);
    }

    public void setActivity(EditorImageActivity activity) {
        mActivity = activity;
    }

    public void reset() {
        mEraseProgress = DEFAULT_PROGRESS;
        mRepairProgress = DEFAULT_PROGRESS;
        mEraseSeekBarShow = true;
        mRepairSeekBarShow = false;
        postDelayed(new Runnable() {
            @Override
            public void run() {
                checkEraseButton();
                showSeekBarWithAnim(mSeekBarLayoutErase, true, false);
                showSeekBarWithAnim(mSeekBarLayoutRepair, false, false);
            }
        }, 300);
        mSettingTopView.setOneStepBtnShow(false);
        mSettingTopView.setTwoStepBtnShow(false);
    }

    @Override
    public void onUndoClick() {
        mEraserView.undo();
    }

    @Override
    public void onResetClick() {
        mEraserView.resetBitmap();
    }

    @Override
    public void onOriginClick(boolean show) {
        mEraserView.setShowSrc(show);
    }

    @Override
    public void onPaintFinish(int size) {
        if (size == 0) {
            mSettingTopView.setOneStepBtnShow(false);
            mSettingTopView.setTwoStepBtnShow(false);
            mActivity.setConfirmEnable(false);
        } else if (size == 1) {
            mSettingTopView.setOneStepBtnShow(true);
            mSettingTopView.setTwoStepBtnShow(false);
            mActivity.setConfirmEnable(true);
        } else if (size == 2) {
            mSettingTopView.setOneStepBtnShow(true);
            mSettingTopView.setTwoStepBtnShow(true);
            mActivity.setConfirmEnable(true);
        } else {
            mSettingTopView.setOneStepBtnShow(true);
            mSettingTopView.setTwoStepBtnShow(true);
            mActivity.setConfirmEnable(true);
        }
    }

    @Override
    public void onTouchDown() {
        showSeekBarWithAnim(mSeekBarLayoutErase, false, true);
        showSeekBarWithAnim(mSeekBarLayoutRepair, false, true);
        mEraseSeekBarShow = false;
        mRepairSeekBarShow = false;
    }

    @Override
    public void onProgressChanged(CustomSizeSeekBar seekBar, int progress, boolean fromUser) {
        if (mEraseButton.isChecked()) {
            mEraseProgress = progress;
            if (mEraserView != null) {
                mEraserView.setPaintWidthErase(mEraseProgress);
            }
        } else if (mRepairButton.isChecked()) {
            mRepairProgress = progress;
            if (mEraserView != null) {
                mEraserView.setPaintWidthRepair(mRepairProgress);
            }
        }
    }

    @Override
    public void onStartTrackingTouch(CustomSizeSeekBar seekBar) {
        if (mEraserView != null) {
            mEraserView.setDrawPaintWidthCircle(true);
        }
    }

    @Override
    public void onStopTrackingTouch(CustomSizeSeekBar seekBar) {
        if (mEraserView != null) {
            mEraserView.setDrawPaintWidthCircle(false);
        }
    }
}
